//package com.mycompany.app.common.scheduler.quartz;
//
//
//import java.util.Date;
//
//import org.hibernate.FlushMode;
//import org.hibernate.Session;
//import org.hibernate.SessionFactory;
//import org.quartz.Job;
//import org.quartz.JobExecutionContext;
//import org.quartz.JobExecutionException;
//import org.springframework.orm.hibernate3.SessionFactoryUtils;
//import org.springframework.orm.hibernate3.SessionHolder;
//import org.springframework.transaction.support.TransactionSynchronizationManager;
//
//import com.mycompany.app.ServiceConstant;
//import com.mycompany.app.common.scheduler.dao.IDaemonLogDao;
//import com.mycompany.app.common.scheduler.model.DaemonLog;
//import com.mycompany.app.util.SpringUtils;
//import com.seagate.service.daemon.scheduler.IDaemonExecuter;
//import com.seagate.service.daemon.scheduler.ScheduleType;
//
//
//public class BaseJob implements Job {
//    protected final org.apache.commons.logging.Log log =
//        org.apache.commons.logging.LogFactory.getLog(this.getClass());
//
//    public void execute(JobExecutionContext jobCtx)
//            throws JobExecutionException {
//
//        //        SpringUtils springUtil = (SpringUtils)SpringUtils.getApplicationContext().getBean("springUtil");
//        IDaemonLogDao daemonLogDao = (IDaemonLogDao) SpringUtils
//                .getApplicationContext()
//                .getBean(ServiceConstant.DAEMON_LOG_DAO);
//        //        IDaemonExecuter daemonExecuter = (IDaemonExecuter)jobCtx.getJobDetail().getJobDataMap().get(PARAM_DAEMON_EXECUTER);
//
//        SessionFactory sessionFactory = null;
//        Session session = null;
//
//        DaemonLog daemonLog = null;
//        Object result = null;
//
//        try {
//            // 1.init and bind global transactional resources.
//            sessionFactory = (SessionFactory) SpringUtils
//                    .getApplicationContext().getBean(
//                            ServiceConstant.SESSION_FACTORY);
//            session  = SessionFactoryUtils.getSession(sessionFactory, true);
//            session.setFlushMode(FlushMode.COMMIT);
//            TransactionSynchronizationManager.bindResource(sessionFactory,
//                    new SessionHolder(session));
//
////             2.check whether this job ever ran or not. 
//            if (existDaemonLog(jobCtx, runningHistory, daemonExecuter))
//                return;
////
////            // checkDaemonPriority();
////            daemonLog = logDaemonStart(jobCtx, runningHistory, daemonExecuter);
//
//            // 3.run job and log result.
////            result = daemonExecuter.onExecute(jobCtx);
////
////            daemonLog.setStatus(TaskStatus.COMPLETED);
////            daemonLog.setDesc(result.toString());
//        } catch (Exception e) {
//            result = e.getMessage();
//
////            if (null != daemonLog) {
////                daemonLog.setStatus(TaskStatus.FAILED);
////                daemonLog.setDesc(e.getMessage());
////            }
//
////            log.error(e.getMessage(), e);
//        } finally {
////            if (null != daemonLog) {
////                logDaemonEnd(jobCtx, runningHistory, daemonLog);
////            }
//
//            // 4.release transactional resources to prevent resource leak.
//            if (null != sessionFactory) {
//                TransactionSynchronizationManager
//                        .unbindResource(sessionFactory);
//                closeSession(session, sessionFactory);
//            }
//        }
//
//    }
//    
//    private boolean existDaemonLog(JobExecutionContext jobCtx, ITaskRunningHistoryService runningHistory, 
//            IDaemonExecuter daemonExecuter) {
//    // ignore RIGHT_NOW task.
//    if( daemonExecuter.getSchedulerType() == ScheduleType.UNDEFINED ) {
//        log.info("Ignore daemon task, schedule type is undefined.");            
//        return true;
//    }
//     
//    int daemonId = daemonExecuter.getDaemonId();
//    int daemonType = daemonExecuter.getDaemonType();
//    Date fireTime = calcScheduledFireTime(jobCtx);
//    
//    
//    DaemonLog daemonLog = runningHistory.queryDaemonLog(daemonId, daemonType, fireTime);
//    if( daemonLog != null ) {
//        log.error("Daemon job has run before! id is " + daemonId + ", type is " + daemonType 
//            + ", fire time is " + fireTime +  ". This trigger will be ignored.");
//        
//        return true;
//    }
//    
//    return false;
//}
//
//}
